﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mall.Infrastructure.Model;
using Mall.Infrastructure.Repository;
using Sx.Mall.WebContract;
using Sx.Mall.WebContract.Dto;
using Sx.Mall.Domain;
using Sx.Mall.WebService.Common;

namespace Sx.Mall.WebService
{
   public class MallUserService :IMallUserService
    {
        AppDomain app = AppDomain.CurrentDomain;

        /// <summary>
        /// 用户注册
        /// </summary>
        public CommonResult Add(MallUserDto dto)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext cxt = app.GetRepositoryContext())
            {
                var repository = cxt.GetRepository<MallUser>();
                var entity = dto.Convert<MallUserDto, MallUser>();
                result = MallUser.Add(repository, entity);
                cxt.Commit();
            }
            return result;
        }

       /// <summary>
       /// 查找人员
       /// </summary>
       /// <param name="uid"></param>
       /// <returns></returns>
        public MallUserDto GetPartUserById(int uid) 
        {
            if (uid > 0)
            {
                using (IRepositoryContext cxt = app.GetRepositoryContext())
                {
                    var repository = cxt.GetRepository<MallUser>();
                    var user = repository.Where(m => m.UserId == uid).First().Convert<MallUser, MallUserDto>();
                    return user;
                }
            }
            return null;
        }
    }
}
